package util;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
/**
 * 电脑截图，开关机工具
 * @author Administrator
 *
 */
public class ComputeUtil {
	public static long curTime;
	public static String curStr;
	public static final String urlString = "http://t.qq.com/weibok2071";
	public static long l;
	public static String msg;

	public static String getDocumentAt(String urlString) {
		StringBuffer document = new StringBuffer();
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));
			String line = null;
			while ((line = reader.readLine()) != null)
				document.append(line + "\n");
			reader.close();

		} catch (MalformedURLException e) {
			System.out.println("Unable to connect to URL: " + urlString);
		} catch (IOException e) {
			System.out.println("IOException when connecting to URL: "
					+ urlString);
		}
		return document.toString();
	}

	/**
	 * 获取发表时间
	 * 
	 * @param str
	 * @return
	 */
	public static long getQQWebTime(String str) {
		str = Util.getMatchString(str, "rel=\"", "\" from=\"");
		str = str.substring(5, 15) + "000";
		long l = Long.parseLong(str);
		return l;
	}

	/**
	 * 获取微博内容
	 * 
	 * @param str
	 * @return
	 */
	public static String getQQWebContent(String str) {
		str = Util.getMatchString(str, "class=\"msgCnt\">", "</div>");
		return str;
	}

	public static void main(String[] args) {
		ComputeUtil client = new ComputeUtil();
		String s = getDocumentAt(urlString);
		// 初始化时间
		curTime = getQQWebTime(s);
		s = null;
		// 开始循环执行
		client.runAuto();

	}

	public void runAuto() {
		int delay = 60000;// 第一次延迟
		int period = 60000;// 运行间隔;86400 000day; 3600 000 hour; 60 000 minute
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			// int i = 0;
			public void run() {
				try {// 为防止线程挂掉，异常全部捕抓掉
					curStr = getDocumentAt(urlString);
					l = getQQWebTime(curStr);
					if (l > curTime) {
						curTime = l;
						msg = getQQWebContent(curStr);
						System.out.println(msg);
						if (msg.indexOf("关机") != -1) {
							String commandStr = "shutdown -s -t 120 -c \" 很晚了，该睡觉了,2 分钟后关闭计算机。\"";
							Runtime.getRuntime().exec(commandStr);
						} else if (msg.indexOf("截图") != -1) {
							printScreen();
							sendScreen();
						}
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, delay, period);

	}

	/**
	 * 截图
	 */
	public static void printScreen() {
		File tempFile = new File("R:", "temp.png");

		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle rect = new Rectangle(d);
		GraphicsEnvironment environment = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		GraphicsDevice device = environment.getDefaultScreenDevice();
		Robot robot;
		BufferedImage desktopImg = null;
		try {
			robot = new Robot(device);
			rect.width = 610;
			rect.height = 500;
			desktopImg = robot.createScreenCapture(rect);
		} catch (AWTException e) {
			e.printStackTrace();
		}
		try {
			ImageIO.write(desktopImg, "png", tempFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 发送邮件
	 */
	public static void sendScreen() {

		MailUtils sendmail = new MailUtils();

		sendmail.setHost("smtp.163.com");

		sendmail.setUserName("liuxunfei@163.com");

		sendmail.setPassWord("19840816");

		sendmail.setTo("28348431@qq.com");

		sendmail.setFrom("liuxunfei@163.com");

		sendmail.setSubject("Screen");

		sendmail.setContent("1");

		// Mail sendmail = new

		// Mail("dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926","你好","胃，你好吗？");

		sendmail.attachfile("R:\\temp.png");

		// sendmail.attachfile("d:\\jhjl.rar");

		System.out.println(sendmail.sendMail());

	}
}